package Q1;

import java.util.ArrayList;
import java.util.Scanner;

// 学生信息管理系统
public class StudentSystem {

    private StudentData db;

    public StudentSystem() {
        this.db = new StudentData();
    }

    public StudentSystem(ArrayList<Student> studentArr) {
        this.db = new StudentData(studentArr);
    }


    //遍历所有元素
    public void show(ArrayList<Student> studentArr) {
        for (Student s : studentArr) {
            System.out.println("ID=" + s.getID() + ", name=" + s.getName() + ", age=" + s.getAge());
        }

  }
    private int getIntegerInput(Scanner sc, String field) {
        while (true) {
            System.out.println("请输入学生" + field);
            String input = sc.next();
            if (input.matches("[0-9]+")) {
                return Integer.parseInt(input);
            } else System.out.println("非法输入");
        }
    }

    private String getStringInput(Scanner sc, String field) {
        System.out.println("请输入学生" + field);
        return sc.next();
    }

    private void showMain() {
        System.out.println("------------欢迎使用学生信息管理系统--------------");
        System.out.println("                 主界面                         ");
        System.out.println("            （1）添加学生信息                    ");
        System.out.println("            （2）删除学生信息                    ");
        System.out.println("            （3）修改学生信息                    ");
        System.out.println("            （4）查找学生信息                    ");
        System.out.println("            （5）打印学生信息                    ");
        System.out.println("            （6）退出系统                    ");
        System.out.println("请输入数字进入相应的功能");
    }

    private void function1(Scanner sc) throws IDException, AgeException {
        //int ID = getIntegerInput(sc, "ID");
        //String name = getStringInput(sc, "姓名");
        //int Age = getIntegerInput(sc, "年龄");
        System.out.println("请输入学生ID");
        int ID = sc.nextInt();
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int Age = sc.nextInt();
        db.addStudent(new Student(ID, name, Age));
    }

    private void delete(Scanner sc) {
        int ID = getIntegerInput(sc, "ID");
        for (Student s : db.studentArr) {
            if (ID == s.getID()) {
                db.deleteStudent(s);
                return;
            }
        }
        System.out.println("没有这样的学生");
    }

    private void replace(Scanner sc) throws IDException, AgeException {
        //System.out.println("新学生：");
        //int ID = getIntegerInput(sc, "ID");
        System.out.println("请输入需要修改学生的ID");
        int ID = sc.nextInt();
        Student dst = db.QueryID(ID);
        if (dst != null) {
            System.out.println("请修改学生姓名");
            String name = sc.next();
            System.out.println("请修改学生年龄");
            int Age = sc.nextInt();
            Student src = new Student(ID, name, Age);
            db.replaceStudent(src, dst);
        } else {
            System.out.println("没有找到到被修改的学生，取消操作");
        }

    }

    private void filter(Scanner sc) {
        System.out.println("(1)学号查找         （2）姓名查找         （3）年龄查找");
        switch (sc.nextInt()) {
            case 1: {
                System.out.println("请输入学号：");
                String content = sc.next();
                show(db.filterStudent("ID", content));
                break;
            }
            case 2: {
                System.out.println("请输入姓名：");
                String content = sc.next();
                show(db.filterStudent("name", content));
                break;
            }
            case 3: {
                System.out.println("请输入年龄：");
                String content = sc.next();
                show(db.filterStudent("age", content));
                break;
            }
        }
    }

    private void function5(Scanner sc) {
        show(db.studentArr);
    }

    private void waiting(Scanner sc) {
        System.out.println("输入任意字符继续。。。");
        sc.next();
    }
    public void run() {
        // 主界面
        Scanner sc = new Scanner(System.in);
        db.load();
        outer:while (true) {
            showMain();
            try {
                switch (sc.nextInt()) {
                    case 1: {
                        function1(sc);
                        break;
                    }
                    case 2: {
                        delete(sc);
                        break;
                    }
                    case 3: {
                        replace(sc);
                        break;
                    }
                    case 4: {
                        filter(sc);
                        break;
                    }
                    case 5: {
                        function5(sc);
                        break;
                    }
                    case 6:
                        {sc.close();break outer;}
                }
            } catch (IDException | AgeException e) {
                e.printStackTrace();
            }
            waiting(sc);
        }
        db.save();
    }
}
